package com.laizt.client.openexe;


import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class ClientTest extends Thread{
    //定义一个Socket对象
    Socket socket = null;
    public void run() {
        //客户端一连接就可以写数据给服务器了
        new ClientTest.sendMessThread().start();
    }


    public ClientTest(String host, int port) {
        try {
            //需要服务器的IP地址和端口号，才能获得正确的Socket对象
            socket = new Socket(host, port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //往Socket里面写数据，需要新开一个线程
    class sendMessThread extends Thread{
        @Override
        public void run() {
            super.run();
            //写操作
            OutputStream os= null;
            try {
                os= socket.getOutputStream();
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("ip","10.21.10.5");

             //   map.put("remoteUrl","\\\\10.21.10.5\\testshare");
                map.put("remoteIp","11.11.11.1");
                map.put("user","cmes");
                map.put("pwd","semc");
               map.put("remoteUrl","\\\\192.168.7.253\\Routeway\\TEST\\IMS\\X3-SE\\X3-H0602-IMS");

                map.put("dir","D:\\N9M-TestTool");
                map.put("port","8881");
                //返回的
                String str = JSONUtils.toJSONString(map);
                os.write(str.getBytes());
                os.flush();

                InputStream in =  socket.getInputStream();
                byte[]  b = new byte[2048];
                in.read(b);
                String msg = new String(b,"UTF-8").trim();
                Map<String,Object> map2 = (Map<String, Object>) JSONObject.parse(msg);
                System.out.println(map2);

                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //函数入口
    public static void main(String[] args) {
        //需要服务器的正确的IP地址和端口号

        ClientTest clientTest=new ClientTest("127.0.0.1", 8885);
       // ClientTest clientTest=new ClientTest("10.21.10.6", 8885);
        clientTest.start();
    }
}
